package reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * 使用特定的构造器实例化对象
 */
public class ReflectDemo3 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Person p1 = new Person();
        System.out.println(p1);

        Person p2 = new Person("李四",20);
        System.out.println(p2);
        System.out.println("--------------------下一位--------------------");

        //加载类对象
        Class cls = Class.forName("reflect.Person");

        //1.通过类对象获取特定的构造器
//        Constructor c = cls.getConstructor();//获取无参构造器
        //2.Person(String name,int age)
        Constructor c = cls.getConstructor(String.class,int.class);

        Object o = c.newInstance("王五",30);
        System.out.println(o);




    }
}
